#! usr/bin/perl
use Bio::SearchIO; 

#/data1/bsi/BORA_processing/NanoBead_BlastAlign/
$filename = $ARGV[0];
if(!$filename){print "You need to enter Blast Results file!\n"; exit;}
$output = $ARGV[1];
if(!$output){print "You need to destination file (second) for Parse!\n"; exit;}
## set your delimintor
$d = "\t";

open(LOG, ">", "ErrLog.txt") || "Cannot Open Log\n";
open(OUT, ">", $output) || die "Cannot Create File: $output\n";
my $in = new Bio::SearchIO(-format => 'blast', -file => $filename);

print OUT "Query".$d."QuerySequence".$d."Hit".$d."HitSequence".$d."TotalLength".$d."Score".$d."PercentIdentity".$d."Evalue".$d."Homology\n";

$name = "xo";

while( my $result = $in->next_result ) {
  ## $result is a Bio::Search::Result::ResultI compliant object
 eval{
  while( my $hit = $result->next_hit ) {
    ## $hit is a Bio::Search::Hit::HitI compliant object  
      while( my $hsp = $hit->next_hsp ) {
      ## $hsp is a Bio::Search::HSP::HSPI compliant object
	if($name ne $result->query_name){
		$name = $result->query_name;
	}
	
		print OUT $result->query_name,$d,
			$hsp->query_string,$d,
			$hit->name,$d,
			$hsp->hit_string,$d,
			$hsp->length('total'),$d,
			$hsp->score,$d,
		        $hsp->percent_identity,$d,
			$hsp->expect,$d,
			$hsp->homology_string,"\n";
	
	
      } 

     }

    };#eval
	if( $@ ){ # an error occurred
	    print LOG  "Full error Occured On Query: $name\n\n$@\n";
	    #exit(-1);
	}
 
  
}
close(OUT);
print "Finished!\n";




